home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / cpphtp2.jar / chpt_12.gml < prev    next >
Text File  |  1998-03-03  |  59KB  |  1,487 lines

  1. <html>
  2. <chapter>
  3. <section type=Popup name=Terminology title="Terminology">
  4. <page>
  5. <font size=14>
  6. A<br>
  7. angle brackets (<b><</b> and <b>></b>) 
  8.  
  9. <a href="%s2p1"><img src=iicons/bullbib.gif></a>
  10. <br>
  11. C<br>
  12. class template 
  13. <a href="%s1p1"><img src=iicons/bullbib.gif></a>
  14. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  15. <br>
  16. class template header 
  17. <a href="%s4p5"><img src=iicons/bullbib.gif></a>
  18. <br>
  19. F<br>
  20. formal parameter in a 
  21. function template 
  22. definition 
  23. <a href="%s2p4"><img src=iicons/bullbib.gif></a>
  24. <br>
  25. <b>friend</b> 
  26. <a href="%s1p2"><img src=iicons/bullbib.gif></a>
  27. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  28. <a href="%s7p1"><img src=iicons/bullbib.gif></a>
  29. <br>
  30. function template 
  31. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  32. <a href="%s2p0"><img src=iicons/bullbib.gif></a>
  33.  
  34. <a href="%s3p0"><img src=iicons/bullbib.gif></a>
  35. <a href="%s4p7"><img src=iicons/bullbib.gif></a>
  36. <br>
  37. K<br>
  38. </font>
  39.  
  40. </page>
  41. <page>
  42. <font size=14>
  43. keyword <b>class</b> 
  44. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  45. <br>
  46. keyword <b>template</b> 
  47. <a href="%s2p1"><img src=iicons/bullbib.gif></a>
  48. <br>
  49. N<br>
  50. non-type parameter 
  51. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  52. <br>
  53. O<br>
  54. overloading template 
  55. functions 
  56. <a href="%s3p0"><img src=iicons/bullbib.gif></a>
  57. <br>
  58. P<br>
  59. parameterized type 
  60. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  61.  
  62. <a href=""><img src=iicons/bullbib.gif></a>
  63. <br>
  64. S<br>
  65. <b>static</b> 
  66. <a href="%s1p2"><img src=iicons/bullbib.gif></a>
  67. <br>
  68. <b>static</b> data member 
  69. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  70. <br>
  71. T<br>
  72. </font>
  73.  
  74. </page>
  75. <page>
  76. <font size=14>
  77. <b>template</b> 
  78. <a href="%s2p1"><img src=iicons/bullbib.gif></a>
  79. <br>
  80. template class 
  81. <a href="^Perform::c:s0p0"><img src=iicons/bullbib.gif></a>
  82. <br>
  83. template function 
  84. <a href="^Perform::c:s0p0"><img src=iicons/bullbib.gif></a>
  85. <br>
  86. type parameter 
  87. <a href="%s2p3"><img src=iicons/bullbib.gif></a>
  88. <a href="%s4p2"><img src=iicons/bullbib.gif></a>
  89. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  90. <br>
  91. <b>typename</b> 
  92. <a href="^Errors::c:s0p0"><img src=iicons/bullbib.gif></a>
  93. <br>
  94. <br>
  95. </font>
  96.  
  97. </page>
  98. </section>
  99. <section type=Popup name=Quotes title="Quotes">
  100. <page>
  101. <i>Behind that outside 
  102. pattern                              
  103. the dim shapes get clearer 
  104. every day.                          
  105. It is always the same 
  106. shape, only very 
  107. numerous.</i>  <br>
  108. Charlotte Perkins Gilman<br>
  109. <br>
  110.  
  111. </page>
  112. <page>
  113. <i>If you are able to slip 
  114. through the parameters   
  115. of the skies and the earth, 
  116. then do so.</i>  <br>
  117. The Koran<br>
  118. <br>
  119.  
  120. </page>
  121. <page>
  122. <i>A Mighty Maze! but not 
  123. without a plan.</i> <br>
  124. Alexander Pope<br>
  125. <br>
  126.  
  127. </page>
  128. </section>
  129. <section type=Popup name=Illustration title="Illustrations">
  130. <page>
  131. <a href="^Code::c:s0p0">Fig. 12.1</a>  A function template.<br>
  132. <a href="^Code::c:s0p1">Fig. 12.2</a>  Using template functions.<br>
  133. <a href="^Code::c:s0p2">Fig. 12.3</a>  Demonstrating class template <b>Stack</b>.<br>
  134. <a href="^Code::c:s0p3">Fig. 12.4</a>  Passing a <b>Stack</b> template object to a function template.<br>
  135. <br>
  136.  
  137. </page>
  138. </section>
  139. <section type=Popup name=Answers title="Answers">
  140. <page pagename="Answer 12.1">
  141. <b>Answer 12.1</b><br>
  142. a) False. It could be a non-template function.<br>
  143. b) False. Each template class will have a copy of the <b>static</b> data member.<br>
  144. c) True.<br>
  145. d) False. Formal parameter names among template functions need not be 
  146. unique.<br>
  147. e) False. Keyword <b>class</b> in this context also allows for a type parameter of a built-
  148. in type.<br>
  149. <foreign  name="exercises" url="^Exercises::c:s0p0">
  150. <br>
  151.  
  152. </page>
  153. <page pagename="Answer 12.2">
  154. <b>Answer 12.2</b><br>
  155. a) template functions, template classes.<br>
  156. b) <b>template</b>, angle brackets (<b><</b> and <b>></b>).<br>
  157. c) overloading.<br>
  158. d) parameterized.<br>
  159. e) binary scope resolution.<br>
  160. f) file.<br>
  161. <foreign  name="exercises" url="^Exercises::c:s0p2">
  162. <br>
  163.  
  164. </page>
  165. <page pagename="Answer 12.9">
  166. <b>Answer 12.9</b><br>
  167. A function template is used to instantiate template functions.<br>
  168. <foreign  name="exercises" url="^Exercises::c:s0p10">
  169. <br>
  170.  
  171. </page>
  172. <page pagename="Answer 12.14">
  173. <b>Answer 12.14</b><br>
  174. If the compiler cannot match the function call made to a template or if the 
  175. matching process results in multiple matches at compile time, the compiler 
  176. generates an error.<br>
  177. <foreign  name="exercises" url="^Exercises::c:s0p15">
  178. <br>
  179.  
  180. </page>
  181. <page pagename="Answer 12.15">
  182. <b>Answer 12.15</b><br>
  183. When creating template classes from a class template, it is necessary to provide a 
  184. type (or possibly several types) to complete the definition of the new type being 
  185. declared. For example, when creating an "array of integers" from an <tt><b>Array</b></tt> class 
  186. template, the type <tt><b>int</b></tt> is provided to the class template to complete the 
  187. definition of an array of integers.<br>
  188. <foreign  name="exercises" url="^Exercises::c:s0p16">
  189. <br>
  190.  
  191. </page>
  192. <page pagename="Answer 12.19">
  193. <b>Answer 12.19</b><br>
  194. To specify at compile time the size of the container class object being declared.<br>
  195. <foreign  name="exercises" url="^Exercises::c:s0p20">
  196. <br>
  197.  
  198. </page>
  199. <page pagename="Answer 12.23">
  200. <b>Answer 12.23</b><br>
  201. For <tt><b>static</b></tt> members of a class template, each template class instantiated 
  202. receives its own copy of all the <tt><b>static</b></tt> members. Then all objects instantiated 
  203. for a given template class access that particular template class's <tt><b>static</b></tt> 
  204. members.<br>
  205. <foreign  name="exercises" url="^Exercises::c:s0p25">
  206. <br>
  207.  
  208. </page>
  209. <page pagename="Answer 12.3">
  210. <b>Answer 12.3</b><br>
  211. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  212. the file cpphtp2/answers/P12_03.zip to your hard drive and unzip the program 
  213. code.<br>
  214. <foreign  name="exercises" url="^Exercises::c:s0p4">
  215. <br>
  216.  
  217. </page>
  218. <page pagename="Answer 12.7">
  219. <b>Answer 12.7</b><br>
  220. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  221. the file cpphtp2/answers/P12_07.zip to your hard drive and unzip the program 
  222. code.<br>
  223. <foreign  name="exercises" url="^Exercises::c:s0p8">
  224. <br>
  225.  
  226. </page>
  227. </section>
  228. <section type=Popup name=Exercises title="Exercises">
  229. <page pagename="Exercise 12.1">
  230. <b>Exercise 12.1</b><br>
  231. Answer each of the following true or false. For those that are false, state why.<br>
  232. a)  A <b>friend</b> function of a function template must be a template function.<br>
  233. b)  If several template classes are generated from a single class template with a 
  234. single <b>static</b> data member, each of the template classes shares a single copy of 
  235. the class template's <b>static</b> data member.<br>
  236. c)  A template function can be overloaded by another template function with the 
  237. same function name.<br>
  238. d)  The name of a formal parameter can be used only once in the formal 
  239. parameter list of the template definition. Formal parameter names among 
  240. template definitions must be unique.<br>
  241. <foreign  name="answers" url="^Answers::c:s0p0">
  242.  
  243. </page>
  244. <page pagename="Exercise 12.1">
  245. e)  Keyword <b>class</b> as used with a template type parameter specifically means 
  246. "any user-defined class type."<br>
  247. <foreign  name="answers" url="^Answers::c:s0p0">
  248. <br>
  249.  
  250. </page>
  251. <page pagename="Exercise 12.2">
  252. <b>Exercise 12.2</b><br>
  253. Fill in the blanks in each of the following:<br>
  254. a)  Templates enable us to specify, with a single code segment, an entire range of 
  255. related functions called ________ , or an entire range of related classes called 
  256. ______.<br>
  257. b)  All function template definitions begin with the keyword ________ 
  258. followed by a list of formal parameters to the function template enclosed in 
  259. ________.<br>
  260. c)  The related functions generated from a function template all have the same 
  261. name, so the compiler uses ________ resolution to invoke the proper function.<br>
  262. d)  Class templates are also called ________ types.<br>
  263. <foreign  name="answers" url="^Answers::c:s0p1">
  264.  
  265. </page>
  266. <page pagename="Ex